package com.zorn.core.util;

public class URLUtils {

	private static final String PROTOCOL = "//", EMPTY = "";
	private static final char DOT = '.', SLASH = '/', PORT = ':', PARAM = '?', HASH = '#';

	public static String getOrigin(String url) {
		return getProtocol(url) + PROTOCOL + getHost(url);
	}

	public static String getProtocol(String url) {
		int index = url.indexOf(PROTOCOL);
		return index == -1 ? EMPTY : url.substring(0, url.indexOf(PROTOCOL));
	}

	public static String getHost(String url) {
		String temp = url.replace(getProtocol(url) + PROTOCOL, EMPTY);
		return temp.substring(0, temp.indexOf(SLASH));
	}

	public static String getPort(String url) {
		String domain = getHost(url);
		int index = domain.indexOf(PORT);
		return index == -1 ? EMPTY : domain.substring(index + 1);
	}

	public static String getPath(String url) {
		return url.replace(getOrigin(url), EMPTY).replace(getHash(url), EMPTY).replace(PARAM + getParameter(url), EMPTY);
	}

	public static String getTarget(String url) {
		String path = getPath(url);
		return path.substring(path.lastIndexOf(SLASH) + 1);
	}

	public static String getSuffix(String url) {
		return getExtention(url);
	}

	public static String getExtention(String url) {
		int index = getTarget(url).indexOf(DOT);
		return index == -1 ? EMPTY : getTarget(url).substring(index + 1);
	}

	public static String getHash(String url) {
		int index = url.indexOf(HASH);
		return index == -1 ? EMPTY : url.substring(index);
	}

	public static String getParameter(String url) {
		int index = url.indexOf(PARAM);
		return index == -1 ? EMPTY : url.substring(index + 1);
	}

}
